<?php

if(isset($_GET['language']))
{
	$language=$_GET['language'];
}

if(isset($_GET['lang']))
{
	if(in_array($_GET['lang'],$Languages))
	{
	   $language       = $_GET['lang'];
		@gpc_setCookie('lang',$language,0,1,"/");
		if(strpos($_SERVER['HTTP_REFERER'],"?lang")===false)
		{
			if(isset($_GET['ref']))
			{
				header("location: /".$_GET['ref']."/");
				exit();
			}
			if(empty($_SERVER['HTTP_REFERER']))
			{
				header("location: /");
				exit();
			}else
			{
				header("location: ".$_SERVER['HTTP_REFERER']);
				exit();
			}
		}
	}
}
if(isset($_COOKIE['lang']))
{
	if(in_array($_COOKIE['lang'],$Languages))
	{
		$language=$_COOKIE['lang'];		
	}
}

function getLang($str)
{
    global $lang,$language;
    if(empty($lang) || !isset($lang))
    {
        $lang           = array();
        @include_once($language.'.txt');
    }
    $url=preg_replace("([^a-zA-Z0-9])","",$str);
    $url=strtoupper($url);
    if(isset($lang[$url]))
    {
        return ($lang[$url]);
    }
    buildLang($url,$str);    
    return ($str);
}

function buildLang($url,$str)
{    
    global $lang,$language;
    if(empty($url) || isset($lang[$url]))return;
    $lang[$url] = $str;
}
function writeLang()
{
    global $language,$lang;
    if(count($lang)==0)return;
    $f=fopen("language/".$language.'.txt','w');
    $data= '<'.'?php'."\n";
    foreach($lang as $k=>$l)
    {
        $l    = mysql_escape_string($l);
        $data.='$lang[\''.$k.'\']= \''.$l.'\';'."\n";
    }
    $data.="?".">";
    @fwrite($f,$data);
    @fclose($f);
}

?>